package com.course.web.page;

import com.course.auth.SecurityUtils;
import com.course.web.view.AccessDeniedView;
import com.course.web.view.ViewDisplayPanel;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.*;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by Justin on 2017/5/30.
 */
@SpringUI(path = "/teach")
public class TeachUI extends UI {

    private final ViewDisplayPanel panel;

    @Autowired
    public TeachUI(ViewDisplayPanel panel) {
        this.panel = panel;
    }

    @Override
    protected void init(VaadinRequest request) {
        HorizontalLayout root = new HorizontalLayout();
        Panel navigation = new Panel("Navigation");
        VerticalLayout action = new VerticalLayout();
        Button logout = new Button("Logout");
        Button home = new Button("Home");

        root.addComponent(navigation);
        root.addComponentsAndExpand(panel);
        navigation.setContent(action);
        action.addComponent(logout);
        action.addComponent(home);
        setContent(root);
        setSizeFull();

        root.setSizeFull();
        navigation.setWidthUndefined();
        panel.setSizeFull();
        panel.setCaption("Teacher Home");

        logout.addClickListener(event -> logout());
        home.addClickListener(event -> home());

        getNavigator().setErrorView(AccessDeniedView.class);
        getPage().setTitle("Teacher Home");
    }

    private void home() {
        getPage().setLocation("/");
    }

    private void logout() {
        SecurityUtils.logout();
        getPage().reload();
        getSession().close();
    }
}
